import cv2
import copy
import numpy as np

from utils.cross_line import check
from config.config_setting import logger, rect_size
from utils import img_util

#人员越界

algo_id = 30


def draw_box_by_data_trt(boxes, scores, label_id, label_dict, img, param, device):
    print(param)
    #rect_size = 1000, 550
    y_len, x_len = img.shape[:2]
    print(y_len, x_len)
    begin_point, end_point = None, None
    if "rule" in list(param.keys()):
        linePoint = param["rule"]["linePoint"]
        begin_point = (int(linePoint[0]["x"]*x_len/rect_size[0]), int(linePoint[0]["y"]*y_len/rect_size[1]))
        end_point = (int(linePoint[1]["x"]*x_len/rect_size[0]), int(linePoint[1]["y"]*y_len/rect_size[1]))
        #begin_point, end_point = [(int(p[0]*int(x_len/rect_size[0])), int(p[1]*int(y_len/rect_size[1]))) for p in linePoint]
    cv2.line(img, begin_point, end_point, (0, 0, 255), 3)
    #print(begin_point, end_point)

    save = False

    for (box, score, idx) in zip(boxes.tolist(), scores.tolist(), label_id.tolist()):
        if idx in label_dict:
            label = label_dict[idx]
            if score > 0.65 and label == 'person':
                #print(score, label)
                c1, c2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
                if begin_point and end_point and check(begin_point, end_point, c1, c2):
                    #print("222")
                    save = True
                    cv2.rectangle(img, c1, c2, (0, 0, 255), thickness=3)
                    logger.info('==='*8 + 'cross_line_box')
                else:
                    cv2.line(img, begin_point, end_point, (0, 0, 255), 3)
    # 对识别结果做去重操作
    if save:
        ori_img = copy.deepcopy(img)
        img_util.save_image(img, device, algo_id, ori_img)
    return img


if __name__ == '__main__':
    pass
